package com.slkj.shilixiaoyuanapp.ui.teacher.tool.PunchClock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

/**
 * 下标标记的日历控件
 * Created by huanghaibin on 2017/11/15.
 */

public class IndexMonthView extends MonthView {
    private Paint mSchemeBasicPaint = new Paint();
    private int mPadding;
    private int mH, mW;
    private int mRadius;
    /**
     * 背景圆点
     */
    private Paint mPointPaint = new Paint();
    /**
     * 圆点半径
     */
    private float mPointRadius;
    public IndexMonthView(Context context) {
        super(context);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setColor(0xff333333);
        mSchemeBasicPaint.setFakeBoldText(true);
        mPadding = dipToPx(getContext(), 4);
        mH = dipToPx(getContext(), 2);
        mW = dipToPx(getContext(), 8);
        mPointRadius = dipToPx(context, 2);
        mPointPaint.setAntiAlias(true);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSchemePaint.setStyle(Paint.Style.STROKE);

    }

    /**
     * 绘制选中的天数背景
     *
     * @param canvas
     * @param calendar
     * @param x
     * @param y
     * @param hasScheme
     * @return
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        mSelectedPaint.setColor(Color.parseColor("#D9EEFF"));
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        return false;
    }

    /**
     * 绘制标记的事件的日子
     * @param canvas
     * @param calendar
     * @param x
     * @param y
     */
    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        mPointPaint.setColor(calendar.getSchemeColor());
        canvas.drawCircle(x + mItemWidth / 2, y + mItemHeight - 3 * mPadding, mPointRadius, mPointPaint);
    }

    /**
     * 绘制文本
     * @param canvas
     * @param calendar
     * @param x
     * @param y
     * @param hasScheme
     * @param isSelected
     */
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
        canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, mCurMonthTextPaint );
        if (calendar.hasScheme()){
            mPointPaint.setColor(calendar.getSchemeColor());
            canvas.drawCircle(x + mItemWidth / 2, y + mItemHeight - 3 * mPadding, mPointRadius, mPointPaint);
        }

        if (calendar.isCurrentDay() && !isSelected) {
            mSelectedPaint.setStyle(Paint.Style.FILL);
            mSelectedPaint.setColor(Color.parseColor("#99D9EEFF"));
            int cx1 = x + mItemWidth / 2;
            int cy1 = y + mItemHeight / 2;
            canvas.drawCircle(cx1, cy1, mRadius, mSelectedPaint);
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, mCurMonthTextPaint );
            if (calendar.hasScheme()){
                mPointPaint.setColor(calendar.getSchemeColor());
                canvas.drawCircle(x + mItemWidth / 2, y + mItemHeight - 3 * mPadding, mPointRadius, mPointPaint);
            }
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
